<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 25, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "html.HtmlControl" );

class HtmlDateTextBoxControl extends HtmlControl {
	
	/**
	 * Create the HTML text for this control
	 *@param string $type
	 * @param string $extAttributes extend attributes of generated control
	 * @param string $id HTML id of this control
	 * 
	 * @return string HTML string
	 */
	function createInputHtml($type ="",$extAttributes = "", $id = null) {
		if (empty ( $id )) {
			$id = $this->_inputName;
		}
		
		SysResponse::addJavascriptFile ( "libraries/js/jquery.date_input.js" );
		SysResponse::addJavascriptFile ( "libraries/js/jquery.date_input.min.js" );
		SysResponse::addStyleSheetFile("/libraries/js/date_input.css");
		$script = '
			$.extend(DateInput.DEFAULT_OPTS, {
			  stringToDate: function(string) {
			    var matches;
			    if (matches = string.match(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/)) {
			      return new Date(matches[3], matches[2] - 1, matches[1]);
			    } else {
			      return null;
			    };
			  },
			
			  dateToString: function(date) {
			    var month = (date.getMonth() + 1).toString();
			    var dom = date.getDate().toString();
			    if (month.length == 1) month = "0" + month;
			    if (dom.length == 1) dom = "0" + dom;
			    return dom + "/" + month + "/" + date.getFullYear();
			  }
			});
		';
		SysResponse::addJavascriptContent ( $script );
		
		$script = "	
			$(function() {
				$('#{$id}').date_input();
				
			    $('#{$id}').keypress(function (e) {
			    	this.input;
			    });
			    
			});
		";
		SysResponse::addJavascriptContent ( $script );
		
		if (! empty ( $id )) {
			$id = "id=\"$id\"";
		}
		$result = "<input type=\"$type\" name=\"{$this->_inputName}\" value=\"{$this->_value}\" $id $extAttributes />";
		return $result;
	}
}
?>
